package project.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import project.entity.TLbt;
import project.entity.TOrders;
import project.service.TOrdersService;
import project.util.ResponseUtil;
import project.util.UUIDUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author YJR
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/api/orders/v1")
public class TOrdersController {
    @Resource
    private TOrdersService ordersService;

    @RequestMapping(value = "/wx/addOrUpdate",method = RequestMethod.POST)
    public boolean addOrUpdate(@RequestBody TOrders orders){
        if(StringUtils.isBlank(orders.getcId())){
            orders.setcId(UUIDUtil.getUUID());
        }
        return ordersService.saveOrUpdate(orders);
    }

    @RequestMapping(value = "/wx/getOrders",method = RequestMethod.POST)
    public List<TOrders> wxGetOrders(@RequestBody JSONObject param){
        String wxOpenId = param.getString("wxOpenId");
        QueryWrapper<TOrders> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(wxOpenId)){
            queryWrapper.lambda().eq(TOrders::getcOpenId,wxOpenId);
        }
        queryWrapper.lambda().orderByDesc(TOrders::getdTime);
        return ordersService.list();
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public boolean del(@RequestBody TOrders orders){
        return ordersService.removeById(orders);
    }
    /**
     *  分页查询数据
     */
    @RequestMapping(value="/select",method = RequestMethod.POST)
    public JSONObject selectSpfl(@RequestBody JSONObject param){
        // 获取页数
        Integer current = param.getInteger("current");
        // 订单状态
        Integer ddzt = param.getInteger("ddzt");
        // 订单id
        String id = param.getString("id");
        // 获取轮播图名
        String flmc = param.getString("flmc");
        Page<TOrders> page = new Page<TOrders>();
        page.setSize(10);
        if(current != null){
            page.setCurrent(current);
        }else{
            page.setCurrent(1);
        }

        QueryWrapper<TOrders> wrapper = new QueryWrapper<>();
        if(ddzt != null){
            wrapper.lambda().eq(TOrders::getnStatus,ddzt);
        }
        if(StringUtils.isNotBlank(id)){
            wrapper.lambda().eq(TOrders::getcId,id);
        }
        // 获取查询返回结果
        Page<TOrders> pageSelect = ordersService.page(page, wrapper);
        JSONObject object = new JSONObject();
        object.put("list",pageSelect.getRecords());
        object.put("total",pageSelect.getTotal());
        return ResponseUtil.getSuccessResponseBody(object);
    }

}
